package com.lottery.material.service.portal.config.impl;

import com.lottery.material.dao.conf.AreaDictMapper;
import com.lottery.material.pojo.conf.AreaDict;
import com.lottery.material.service.portal.config.AreaDictService;
import com.lottery.material.vo.portal.area.AreaVo;
import com.lottery.material.vo.portal.area.CityVo;
import com.lottery.material.vo.portal.area.ProvinceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:    区域字典服务实现类
 * Author:         sunny
 * CreateDate:     2019-05-14 16:31
 */
@Service
public class AreaDictServiceImpl implements AreaDictService {

    @Autowired
    AreaDictMapper areaDictMapper;

    /**
     * description: 插入省市区
     * author sunny
     * createDate 2019-05-14 下午 4:32
     * @param record
     */
    @Override
    public void insertArea(AreaDict record) {
        areaDictMapper.insertSelective(record);
    }

    /**
     * description: 查询所有省市区信息
     * author sunny
     * createDate 2019-05-15 下午 3:42
     * @return
     * @throws Exception
     */
    @Override
    public List<ProvinceVo> findAllArea() throws Exception {
        //查询所有省市区信息
        List<AreaDict> AreaList = areaDictMapper.findAll();

        List<ProvinceVo> provinceVos = new ArrayList<>();
        for (int i = 0; i < AreaList.size(); i++){
            AreaDict province = AreaList.get(i);
            if(province.getRegionType() == 1 && province.getParentId().compareTo(86) == 0){
                ProvinceVo provinceVo = new ProvinceVo();
                provinceVo.setCode(province.getRegionId().toString());
                provinceVo.setName(province.getRegionName());
                List<CityVo> cityVos = new ArrayList<>();
                for (int j = 0; j < AreaList.size(); j++){
                    AreaDict city = AreaList.get(j);
                    if(city.getRegionType() == 2 && city.getParentId().compareTo(province.getRegionId()) == 0){
                        CityVo cityVo = new CityVo();
                        cityVo.setCode(city.getRegionId().toString());
                        cityVo.setName(city.getRegionName());
                        List<AreaVo> areaVos = new ArrayList<>();
                        for(int k =0 ; k<AreaList.size();k++){
                            AreaDict area = AreaList.get(k);
                            if(area.getRegionType() == 3 && area.getParentId().compareTo(city.getRegionId()) == 0){
                                AreaVo areaVo = new AreaVo();
                                areaVo.setCode(area.getRegionId().toString());
                                areaVo.setName(area.getRegionName());
                                areaVos.add(areaVo);
                            }
                        }
                        cityVo.setAreaList(areaVos);
                        cityVos.add(cityVo);
                    }
                }
                provinceVo.setCityList(cityVos);
                provinceVos.add(provinceVo);
            }
        }
        return provinceVos;
    }
}
